package com.mes.cloud.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.area.BaseAreaOrgPageRequest;
import com.mes.cloud.api.dto.request.organization.SysOrganizationAreaRequest;
import com.mes.cloud.api.dto.response.organization.SysOrganizationAreaDto;
import com.mes.cloud.api.dto.response.organization.SysOrganizationDto;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.dao.SysOrganizationAreaDao;
import com.mes.cloud.entity.SysOrganization;
import com.mes.cloud.entity.SysOrganizationArea;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织行政区中间表
 *
 * @author xy
 * @version 2019-08-01 10:47:27
 * @email yuan.xue3@pactera.com
 */
@Service
public class SysOrganizationAreaService {

    private SysOrganizationAreaDao sysOrganizationAreaDao;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    public SysOrganizationAreaService(SysOrganizationAreaDao sysOrganizationAreaDao) {
        this.sysOrganizationAreaDao = sysOrganizationAreaDao;
    }

    /**
     * <功能详细描述>
     *
     * @param sysOrganizationAreaRequest
     * @return java.util.List<com.shinho.crm.base.api.dto.response.organization.SysOrganizationAreaDto>
     * @author xy
     * @date 2019/8/9 10:20
     */
    public List<SysOrganizationAreaDto> listByOrganizationId(SysOrganizationAreaRequest sysOrganizationAreaRequest) {
        //查询条件
        SysOrganizationArea selectParam = new SysOrganizationArea();
        BeanUtils.copyProperties(sysOrganizationAreaRequest, selectParam);

        List<SysOrganizationArea> sysOrganizationAreaList = sysOrganizationAreaDao.select(selectParam);
        List<SysOrganizationAreaDto> sysOrganizationAreaDtoList = new ArrayList<>();
        for (SysOrganizationArea sysOrganizationArea : sysOrganizationAreaList) {
            SysOrganizationAreaDto sysOrganizationAreaDto = new SysOrganizationAreaDto();
            BeanUtils.copyProperties(sysOrganizationArea, sysOrganizationAreaDto);
            sysOrganizationAreaDtoList.add(sysOrganizationAreaDto);
        }

        return sysOrganizationAreaDtoList;
    }

    /**
     * <功能详细描述>
     *
     * @param sysOrganizationAreas
     * @return int
     * @author xy
     * @date 2019/8/9 10:20
     */
    public int batchInsert(List<SysOrganizationArea> sysOrganizationAreas) {
        return sysOrganizationAreaDao.batchInsert(sysOrganizationAreas);
    }

    /**
     * <通过组织id更新>
     *
     * @param sysOrganizationArea
     * @return int
     * @author xy
     * @date 2019/8/9 10:19
     */
    public int updateByOrganizationId(SysOrganizationArea sysOrganizationArea) {
        return sysOrganizationAreaDao.updateByOrganizationId(sysOrganizationArea);
    }

    /**
     * @description 根据行政区划查找组织
     * @author dongyi
     * @date 2019-08-02 11:32
     **/
    public CommPager<SysOrganizationDto> selectOrgByArea(BaseAreaOrgPageRequest pageRequest) {
        Page<SysOrganizationAreaDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        Map<String, Object> param = new HashMap<>(2);
        param.put("areaId", pageRequest.getAreaId());
        param.put("queryString", pageRequest.getQueryString());
        List<SysOrganization> orgList = sysOrganizationAreaDao.selectOrgByArea(param);

        List<SysOrganizationDto> organizationDtoList = new ArrayList<>();
        for (SysOrganization sysOrganization : orgList) {
            SysOrganizationDto sysOrganizationDto = new SysOrganizationDto();
            BeanUtils.copyProperties(sysOrganization, sysOrganizationDto);
            organizationDtoList.add(sysOrganizationDto);
        }

        CommPager<SysOrganizationDto> pageResult = new CommPager<>(page, organizationDtoList);

        return pageResult;
    }
}
